//
//  Networking.swift
//  sina
//
//  Created by 江先铨 on 15/11/17.
//  Copyright © 2015年 jxqstatic. All rights reserved.
//

import UIKit
import AFNetworking


private let weiboDomain = "com.baidu.data.error"

enum HTTPMethod: String {
   case POST = "POST"
   case GET = "GET"
}

class Networking: AFHTTPSessionManager {

    //定义一个单利对象
    static let sharedTools: Networking = {
    
         let urlString = "https://api.weibo.com/"
         let url = NSURL(string: urlString)
        //创建一个url 的网络请求数据
         let tools = Networking(baseURL: url)
        //插入文本的类型
         tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
         return tools
    }()
    
    func requestJsonDict(method: HTTPMethod, urlString: String, parmaters: [String: AnyObject]?, finshed: (result: [String : AnyObject]? , error: NSError?) -> ()) {
        if method == HTTPMethod.GET {
        
        GET(urlString, parameters: parmaters, success: { (_, result) -> Void in
            //检验数据是否是字典
            if let dict = result as? [String : AnyObject] {
              //执行网络之间的回调
               finshed(result: dict, error: nil)
                return
            }
            
            //数据格式不匹配
            //自定义错误信息
            //一般自定义errorcode 为负值
            let dataError = NSError(domain: weiboDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据格式错误"])
            
            finshed(result: nil, error: dataError)
            
            }, failure: { (_, error) -> Void in
                
                finshed(result: nil, error: error)
        })
            
        } else {
          POST(urlString, parameters: parmaters, success: { (_, result) -> Void in
                if let dict = result as? [String : AnyObject] {
                    //执行网络之间的回调
                    finshed(result: dict, error: nil)
                    return
                }
                
                //数据格式不匹配
                //自定义错误信息
                //一般自定义errorcode 为负值
                let dataError = NSError(domain: weiboDomain, code: -10000, userInfo: [NSLocalizedDescriptionKey : "数据格式错误"])
                
                finshed(result: nil, error: dataError)
                
                }, failure: { (_, error) -> Void in
                    //执行错误的回调
                    finshed(result: nil, error: error)
                    print(error)
            })

        }
    }
    
    
    //添加上传图片的方法
    func uploadImage(urlString: String, parmaters: [String : AnyObject]?, imageData: NSData, finished: (result: [String : AnyObject]?, error : NSError?) -> ()){
    
       
       POST(urlString, parameters: parmaters, constructingBodyWithBlock: { (multipartFormData) -> Void in
        //data 要上传的文件的二进制数据
        //name 表示上传文件的服务器对应的字段
        //filename 表示服务器接受后 存储的名字 名字可以随便取
        //上传文件的格式
       multipartFormData.appendPartWithFileData(imageData, name: "pic", fileName: "OMG", mimeType: "image/jpeg")
        
        
        }, success: { (_, reuslt) -> Void in
            
            if let dict = reuslt as? [String : AnyObject]  {
            
               finished(result: dict, error: nil)
            }
            
        }) { (_, error) -> Void in
            
            finished(result: nil, error: error )
        }
    }
}
